package com.my.adminservice.config.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.adminapi.config.domain.dto.*;
import com.my.adminapi.config.domain.vo.DictionaryDataVo;
import com.my.adminapi.config.domain.vo.DictionaryTypeVO;
import com.my.adminservice.config.domain.entity.SysDictionaryData;
import com.my.adminservice.config.domain.entity.SysDictionaryType;
import com.my.adminservice.config.mapper.SysDictionaryDataMapper;
import com.my.adminservice.config.mapper.SysDictionaryTypeMapper;
import com.my.adminservice.config.service.ISysDictionaryService;
import com.my.mycommondomain.domian.vo.BasePageVO;
import com.my.mycommondomain.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SysDictionaryServiceImpl implements ISysDictionaryService {
    @Autowired
    private SysDictionaryTypeMapper sysDictionaryTypeMapper;

    @Autowired
    private SysDictionaryDataMapper sysDictionaryDataMapper;
    /**
     * 增加字典类型
     * @param dictionaryTypeWriteReqDTO  请求的入参
     * @return 主键id
     */
    @Override
    public Long addType(DictionaryTypeWriteReqDTO dictionaryTypeWriteReqDTO) {
        LambdaQueryWrapper<SysDictionaryType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(SysDictionaryType::getId).eq(SysDictionaryType::getValue,dictionaryTypeWriteReqDTO.getValue())
                .or().eq(SysDictionaryType::getTypeKey,dictionaryTypeWriteReqDTO.getTypeKey());
        SysDictionaryType sysDictionaryType = sysDictionaryTypeMapper.selectOne(queryWrapper);
        if(sysDictionaryType!=null){
            throw new ServiceException("key或者value不存在");
        }
        sysDictionaryType = new SysDictionaryType();
        sysDictionaryType.setValue(dictionaryTypeWriteReqDTO.getValue());
        sysDictionaryType.setTypeKey(dictionaryTypeWriteReqDTO.getTypeKey());
        if(StringUtils.isNotBlank(dictionaryTypeWriteReqDTO.getRemark())){
            sysDictionaryType.setRemark(dictionaryTypeWriteReqDTO.getRemark());
        }
        int result = sysDictionaryTypeMapper.insert(sysDictionaryType);
        if(result!=1){
            throw new ServiceException("字典类型键插入失败");
        }
        return sysDictionaryType.getId();
    }

    /**
     * 字典列表
     * @param sysDictionaryTypeListReqDTO 请求参数
     * @return 字典列表
     */
    @Override
    public BasePageVO<DictionaryTypeVO> listType(DictionaryTypeListReqDTO sysDictionaryTypeListReqDTO) {
        LambdaQueryWrapper<SysDictionaryType> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(sysDictionaryTypeListReqDTO.getKeyType())){
            queryWrapper.likeRight(SysDictionaryType::getTypeKey,sysDictionaryTypeListReqDTO.getKeyType());
        }
        if(StringUtils.isNotBlank(sysDictionaryTypeListReqDTO.getValue())){
            queryWrapper.eq(SysDictionaryType::getValue,sysDictionaryTypeListReqDTO.getValue());
        }
        Page<SysDictionaryType> result= sysDictionaryTypeMapper.selectPage(new Page<>(sysDictionaryTypeListReqDTO.getPageNo().longValue(),sysDictionaryTypeListReqDTO.getPageSize().longValue()),queryWrapper);
        if(result==null){
            log.error("分页查询字典列表失败");
            throw new ServiceException("分页查询字典列表失败");
        }
        BasePageVO<DictionaryTypeVO> dictionaryTypeVOBasePageVO=new BasePageVO<>();
        List<DictionaryTypeVO> list = new ArrayList<>();
        for(SysDictionaryType sysDictionaryType:result.getRecords()){
            DictionaryTypeVO dictionaryTypeVO=new DictionaryTypeVO();
            BeanUtils.copyProperties(sysDictionaryType,dictionaryTypeVO);
            list.add(dictionaryTypeVO);
        }
        dictionaryTypeVOBasePageVO.setList(list);
        return dictionaryTypeVOBasePageVO;
    }

    @Override
    public Long editType(DictionaryTypeWriteReqDTO dictionaryTypeWriteReqDTO) {
        //首先就是查数据库中是否存在key
        LambdaQueryWrapper<SysDictionaryType> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SysDictionaryType> eq = queryWrapper.eq(SysDictionaryType::getTypeKey, dictionaryTypeWriteReqDTO.getTypeKey());
        SysDictionaryType sysDictionaryType = sysDictionaryTypeMapper.selectOne(eq);
        if(sysDictionaryType==null){
            throw new ServiceException("字典类型不存在");
        }
        //判断字典类型名称是否存在,也就是字典类型值
        LambdaQueryWrapper<SysDictionaryType> eq1 = queryWrapper.ne(SysDictionaryType::getTypeKey,
                dictionaryTypeWriteReqDTO.getTypeKey()).eq(SysDictionaryType::getValue, dictionaryTypeWriteReqDTO.getValue());
        SysDictionaryType sysDictionaryType1 = sysDictionaryTypeMapper.selectOne(eq1);
        if(sysDictionaryType1!=null){
            throw new ServiceException("字典类型名称已经存在");
        }
        //开始写入
        sysDictionaryType.setValue(dictionaryTypeWriteReqDTO.getValue());
        if (dictionaryTypeWriteReqDTO.getRemark()!=null){
            sysDictionaryType.setRemark(dictionaryTypeWriteReqDTO.getRemark());
        }
        sysDictionaryTypeMapper.updateById(sysDictionaryType);


        return sysDictionaryType.getId();
    }

    /**
     * 新增字典数据
     * @param dictionaryDataWriteDTO 入参
     * @return id
     */
    @Override
    public Long addData(DictionaryDataWriteReqDTO dictionaryDataWriteDTO) {
        //先查字典类型是否存在
        LambdaQueryWrapper<SysDictionaryType> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SysDictionaryType> eq = queryWrapper.eq(SysDictionaryType::getTypeKey, dictionaryDataWriteDTO.getTypeKey());
        SysDictionaryType sysDictionaryType = sysDictionaryTypeMapper.selectOne(eq);
        if(StringUtils.isBlank(sysDictionaryType.getTypeKey())){
            throw new ServiceException("字典类型主键不存在");
        }
        //再查字典数据是否存在
        LambdaQueryWrapper<SysDictionaryData> queryWrapper1 = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SysDictionaryData> eq1 = queryWrapper1.eq(SysDictionaryData::getValue, dictionaryDataWriteDTO.getValue()).or().eq(SysDictionaryData::getDataKey, dictionaryDataWriteDTO.getDataKey());
        SysDictionaryData sysDictionaryData1 = sysDictionaryDataMapper.selectOne(eq1);
        if(sysDictionaryData1!=null){
            throw new ServiceException("字典数据键或者值已经存在");
        }
        //开始新增
        SysDictionaryData re=new SysDictionaryData();
        re.setDataKey(dictionaryDataWriteDTO.getDataKey());
        re.setValue(dictionaryDataWriteDTO.getValue());
        re.setTypeKey(dictionaryDataWriteDTO.getTypeKey());
        if(StringUtils.isNotBlank(dictionaryDataWriteDTO.getRemark())){
            re.setRemark(dictionaryDataWriteDTO.getRemark());
        }
        if(dictionaryDataWriteDTO.getSort()!=null){
            re.setSort(dictionaryDataWriteDTO.getSort());
        }
        sysDictionaryDataMapper.insert(re);
        return re.getId();
    }

    @Override
    public BasePageVO<DictionaryDataVo> dataList(DictionaryDataListReqDTO dictionaryDataListReqDTO) {
        BasePageVO<DictionaryDataVo> result=new BasePageVO<>();
        //因为业务主键已经被注解校验,这里不做校验
        LambdaQueryWrapper<SysDictionaryData> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictionaryData::getTypeKey,dictionaryDataListReqDTO.getTypeKey());
        if(StringUtils.isNotBlank(dictionaryDataListReqDTO.getValue())){
            queryWrapper.likeRight(SysDictionaryData::getValue,dictionaryDataListReqDTO.getValue());
        }
        queryWrapper.orderByAsc(SysDictionaryData::getSort);
        queryWrapper.orderByAsc(SysDictionaryData::getId);
        Page<SysDictionaryData> sysDictionaryDataPage = sysDictionaryDataMapper.selectPage(new Page<>(dictionaryDataListReqDTO.getPageNo().longValue(), dictionaryDataListReqDTO.getPageSize().longValue()), queryWrapper);
        if(sysDictionaryDataPage==null){
            throw new ServiceException("没有查找到字典数据列表");
        }
        result.setTotals(((Long)sysDictionaryDataPage.getTotal()).intValue());
        result.setTotalPages(((Long)sysDictionaryDataPage.getPages()).intValue());
        List<DictionaryDataVo> dictionaryDataVoList = new ArrayList<>();
        for(SysDictionaryData sd:sysDictionaryDataPage.getRecords()){
            DictionaryDataVo dictionaryDataVo=new DictionaryDataVo();
            BeanUtils.copyProperties(sd,dictionaryDataVo);
            dictionaryDataVoList.add(dictionaryDataVo);
        }
        result.setList(dictionaryDataVoList);
        return result;
    }

    @Override
    public Long editData(DictionaryDataEditReqDTO dto) {
        //首先就是判断字典数据的键是否存在
        LambdaQueryWrapper<SysDictionaryData> queryWrapper1 = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SysDictionaryData> eq1 = queryWrapper1.eq(SysDictionaryData::getDataKey, dto.getDataKey());
        SysDictionaryData sysDictionaryData = sysDictionaryDataMapper.selectOne(eq1);
        if(sysDictionaryData==null){
            throw new ServiceException("字典数据的键不存在");
        }
        LambdaQueryWrapper<SysDictionaryData> queryWrapper2 = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SysDictionaryData> eq = queryWrapper2.ne(SysDictionaryData::getDataKey, dto.getDataKey()).eq(SysDictionaryData::getValue, dto.getValue());
        SysDictionaryData sysDictionaryData1 = sysDictionaryDataMapper.selectOne(eq);
        if(sysDictionaryData1!=null){
            throw new ServiceException("字典数据名称已存在");
        }
        //走到这里说明是存在的
        sysDictionaryData.setValue(dto.getValue());
        if(StringUtils.isNotBlank(dto.getRemark())){
            sysDictionaryData.setRemark(dto.getRemark());
        }
        if (dto.getSort()!=null){
            sysDictionaryData.setSort(dto.getSort());
        }
        if (dto.getStatus()!=null){
            sysDictionaryData.setStatus(dto.getStatus());
        }
        sysDictionaryDataMapper.updateById(sysDictionaryData);
        return sysDictionaryData.getId();
    }

    @Override
    public List<DictionaryDataDTO> selectDictDataByType(String typeKey) {
        //首先判断typeKey是否存在
        LambdaQueryWrapper<SysDictionaryData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictionaryData::getTypeKey, typeKey);
        List<SysDictionaryData> sysDictionaryDataList = sysDictionaryDataMapper.selectList(queryWrapper);
        if(sysDictionaryDataList==null){
            throw new ServiceException("typeKey不存在");
        }
        List<DictionaryDataDTO> dictionaryDataDTOList = new ArrayList<>();
        for(SysDictionaryData sd:sysDictionaryDataList){
            DictionaryDataDTO dictionaryDataDTO=new DictionaryDataDTO();
            BeanUtils.copyProperties(sd,dictionaryDataDTO);
            dictionaryDataDTOList.add(dictionaryDataDTO);
        }
        return dictionaryDataDTOList;
    }

    @Override
    public Map<String, List<DictionaryDataDTO>> selectDictDataByTypes(List<String> typeKeys) {
        //首先查询typeKeys是否都存在
        LambdaQueryWrapper<SysDictionaryData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysDictionaryData::getTypeKey, typeKeys);
        List<SysDictionaryData> sysDictionaryData = sysDictionaryDataMapper.selectList(queryWrapper);
        if(sysDictionaryData==null){
            throw new ServiceException("传入的keyTypes,其中有可能不存在");
        }
        Map<String, List<DictionaryDataDTO>> dictionaryDataDTOMap = new HashMap<>();
        List<DictionaryDataDTO> result = new ArrayList<>();
        for(SysDictionaryData sd:sysDictionaryData){
            DictionaryDataDTO dictionaryDataDTO=new DictionaryDataDTO();
            BeanUtils.copyProperties(sd,dictionaryDataDTO);
            result.add(dictionaryDataDTO);
        }
        for(DictionaryDataDTO dictionaryDataDTO:result){
            List<DictionaryDataDTO> value;
            if(dictionaryDataDTOMap.get(dictionaryDataDTO.getTypeKey())==null){
                value = new ArrayList<>();
                value.add(dictionaryDataDTO);
                dictionaryDataDTOMap.put(dictionaryDataDTO.getTypeKey(),value);
            }else {
                value = dictionaryDataDTOMap.get(dictionaryDataDTO.getTypeKey());
                value.add(dictionaryDataDTO);
            }
        }
        return dictionaryDataDTOMap;
    }

    @Override
    public DictionaryDataDTO getDicDataByKey(String dataKey) {
        //首次就是查看这个键是否存在
        LambdaQueryWrapper<SysDictionaryData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictionaryData::getDataKey, dataKey);
        SysDictionaryData sysDictionaryData = sysDictionaryDataMapper.selectOne(queryWrapper);
        if(sysDictionaryData==null){
            throw new ServiceException("数据字典键不存在");
        }
        DictionaryDataDTO dictionaryDataDTO=new DictionaryDataDTO();
        BeanUtils.copyProperties(sysDictionaryData,dictionaryDataDTO);
        return dictionaryDataDTO;
    }

    @Override
    public List<DictionaryDataDTO> getDicDataByKeys(List<String> dataKeys) {
        //先查dataKeys是否存在
        LambdaQueryWrapper<SysDictionaryData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysDictionaryData::getDataKey, dataKeys);
        List<SysDictionaryData> sysDictionaryData = sysDictionaryDataMapper.selectList(queryWrapper);
        if(sysDictionaryData==null){
            throw new ServiceException("数据键中可能个别不存在");
        }
        List<DictionaryDataDTO> dtoList = new ArrayList<>();
        for(SysDictionaryData sd:sysDictionaryData){
            DictionaryDataDTO dictionaryDataDTO=new DictionaryDataDTO();
            BeanUtils.copyProperties(sd,dictionaryDataDTO);
            dtoList.add(dictionaryDataDTO);
        }
        return dtoList;
    }


}
